import {Security_Response} from "../axdr/Security_Response";
import {FrameData} from "../bean/FrameData";
import {resultCode} from "../tools/DataTool";
import {ConnectResponseInfo} from "../axdr/ConnectResponseInfo";


export function parseSecurityResponse(security_response: Security_Response): String {
    let frameData = new FrameData();
    switch (security_response.appUnit.getChoiceIndex()) {
        case 0:
            let plain_unit = {
                plain_data: {},
                mac: {},
            }
            plain_unit.plain_data = security_response.appUnit.appUnitPalin.toString();
            if (security_response.dataValid.isUsed()) {
                plain_unit.mac = security_response.dataValid.getValue().mac.toString();
            }
            frameData.data = plain_unit;
            frameData.code = resultCode.success;
            break;
        case 1:
            let cipher_unit = {
                cipher_data: {},
                mac: {},
            }
            cipher_unit.cipher_data = security_response.appUnit.appUnit.toString();
            if (security_response.dataValid.isUsed()) {
                cipher_unit.mac = security_response.dataValid.getValue().mac.toString();
            }
            frameData.data = cipher_unit;
            frameData.code = resultCode.success;
            break;
        case 2:
            let dar = {
                dar: {},
            };
            dar.dar = security_response.appUnit.dar.toString();
            frameData.data = dar;
            frameData.code = resultCode.success;
            break;
    }
    return JSON.stringify(frameData);
}

export function parseConnectResponse(connectResponseInfo: ConnectResponseInfo): String {
    let frameData = new FrameData();
    let conect_data = {
        rn: {},
        sign: {},
    }
    conect_data.rn = connectResponseInfo.securityData.getValue().rn.toString();
    conect_data.sign = connectResponseInfo.securityData.getValue().sign.toString();
    frameData.data = conect_data;
    frameData.code = resultCode.success;
    return JSON.stringify(frameData);
}
